<?php
namespace app\index\controller;

use think\Controller;
use app\admin\model\NewsModel;
use app\admin\model\ClassModel;
use app\admin\model\PageModel;
use app\admin\model\CommentModel;

/**
 * 前台首页控制器
 * @author Alinejun666
 * Date 2018-8-15
 */
class Index extends controller
{
    public function index()
    {
        $news    = new NewsModel();
        $class   = new ClassModel();
        $comment = new CommentModel();

        $where = [
            'home'   => 1,
            'status' => 1,
        ];
        $order = "create_time desc";
        $start = 0;
        $size  = 8;
        $list  = $news->get_list_page($where, $order, $fields = "*", $start, $size);
        foreach ($list as $k => $v) {
            $list[$k]['classname'] = $class->get_field(['class_id' => $v['class_id']], "name");
            // 评论数量
            $list[$k]['count_comment'] = $comment->get_count(['news_id' => $v['news_id']]);
        }

        $data  = [
            'newslist' => $list,
        ];

		return $this->fetch('index', $data);
    }

    public function about()
    {
        $page = new PageModel();

        $info = $page->get_info(['page_id' => 1]);

		return $this->fetch('about/about', ['data' => $info]);
    }


    public function uploadfile(){
        // 文件分类
        $type = input("type","image");
        // 文件保存路径
        $path = "./uploads/".$type;
        // 获取标识image上传的文件
        $file = request()->file('image');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move($path);
        if($info){
            // 成功上传后 获取上传信息
            return json($path."/".$info->getSaveName());
            
        }else{
            // 上传失败获取错误信息
            return json(msg_error($file->getError()));
        }
    }
}
